package com.waiting.try_with_resource;

import java.io.BufferedReader;
import java.io.FileReader;

/**
 * @Description 测试java7的try-with-resource功能
 * @Author bjweitingting
 * @Date 2019/1/29
 **/
public class Main {
    static String readFirstLineFromFile(String path) throws Exception {
        try (BufferedReader br =
                     new BufferedReader(new FileReader(path))) {
            System.out.println("before exception");
            throw new Exception("readFirstLineFromFile中抛出的异常");
            //return br.readLine();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("finally");
            return "finally";
        }
    }

    static String readFirstLineFromFileWithFinallyBlock(String path)
            throws Exception {
        BufferedReader br = new BufferedReader(new FileReader(path));
        try {
            throw new Exception("readFirstLineFromFileWithFinallyBlock- try中抛出的异常");
//            return br.readLine();
        } finally {
            throw new Exception("readFirstLineFromFileWithFinallyBlock- finally中抛出的异常");
//            if (br != null) br.close();
        }
    }

    public static void main(String[] args) {

        try {
            // do something
            System.exit(1);
        } finally{
            System.out.println("Print from finally");
        }
        String path = "C:/Users/bjweitingting/Desktop/test.txt";
        try {
            readFirstLineFromFile(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            readFirstLineFromFileWithFinallyBlock(path);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
